package com.rongcheng.fortestlist;

import android.content.Context;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.WindowManager;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by Administrator on 2016/6/22.
 */
public class Utils {
    /**
     * 得到屏幕宽度
     */
    public static int getScreenWidth() {
        return ((WindowManager) SKApplication.getInstance().getContext().getSystemService(Context.WINDOW_SERVICE))
                .getDefaultDisplay().getWidth();
    }

    public static int getScreenHeight() {
        return ((WindowManager) SKApplication.getInstance().getContext().getSystemService(Context.WINDOW_SERVICE))
                .getDefaultDisplay().getHeight();
    }

    public static int dip2px(Context context, float dipValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dipValue * scale + 0.5f);
    }

    public static int dp2px(Context context, int dpVal) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                dpVal, context.getResources().getDisplayMetrics());
    }

    /**
     * 根据手机的分辨率从 px(像素) 的单位 转成为 dp
     */
    public static int pxTodip(Context context, float pxValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }

    public static String FileMD5Helper(String str) {
        InputStream fis = null;
        try {
            fis = new FileInputStream(str);
            byte[] buffer = new byte[1024 * 512];
            MessageDigest messageDigest = MessageDigest.getInstance("MD5");
            messageDigest.reset();
            int numRead = -1;
            while ((numRead = fis.read(buffer)) != -1) {
                messageDigest.update(buffer, 0, numRead);
            }
            byte[] byteArray = messageDigest.digest();
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < byteArray.length; i++) {
                if (Integer.toHexString(0xFF & byteArray[i]).length() == 1) {
                    sb.append("0").append(Integer.toHexString(0xFF & byteArray[i]));
                } else {
                    sb.append(Integer.toHexString(0xFF & byteArray[i]));
                }
            }
            return sb.toString();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        throw new RuntimeException("no device Id");
    }

    public static String getTimeFromSum(String sum) {
        int number = 0;
        String time = "";
        try {
            number = Integer.parseInt(sum);

            int seconds = number % 60;
            int minutes = number / 60;

            int minutes2 = minutes % 60;
            int hours = minutes / 60;

            time = getStandTimeFromSum(hours) + ":" + getStandTimeFromSum(minutes2) + ":" + getStandTimeFromSum(seconds);
        } catch (Exception e) {
            if (sum.length() > 6) {
                time = sum.substring(0, 6) + "...";
            } else {
                time = sum;
            }
            e.printStackTrace();
        }
        return time;
    }

    private static String getStandTimeFromSum(int sum) {
        String time = "";
        if (sum < 10) {
            time = "0" + sum;
        } else {
            time = "" + sum;
        }
        return time;
    }

    /**
     * 验证注册手机号码是否正确
     */
    public static boolean isMobileNO(String mobiles) {
        if (mobiles == null) {
            return false;
        }
        Pattern p = Pattern.compile("^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$");
        Matcher m = p.matcher(mobiles);
        return m.matches();
    }

    /**
     * 根据不同的id类型，获取不同的组合文字结果
     *
     * @param id   id
     * @param str  已完成
     * @param flag true:已完成个数   false：总个数
     * @return
     */
    public static String getTextById(String id, String str, boolean flag) {
        String result = "";
        switch (id) {
            case "201":
            case "301":
            case "215":
            case "315":
            case "217":
            case "0":
                result = flag ? "已观看" + str + "分钟/" : str + "分钟";
                break;
            case "202":
            case "302":
                result = flag ? "已参加" + str + "个/" : str + "个";
                break;
            case "206":
            case "306":
                result = flag ? "已获得" + str + "分/" : str + "分";
                break;
            case "210":
            case "310":
                result = flag ? "已上传" + str + "个/" : str + "个";
                break;
            case "220":
            case "320":
                result = flag ? "已被点评" + str + "篇/" : str + "篇";
                break;
            case "211":
            case "311":
                result = flag ? "已提问" + str + "个/" : str + "个";
                break;
            default:
                result = flag ? "已完成" + str + "个/" : str + "个";
                break;
        }
        return result;
    }


    public static int convertDpToPx(Context context, int dp) {
        if (context == null)
            return 0;
        DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
        return Math.round(dp * displayMetrics.density);
    }

    public static String getSize(String filesize) {

        String file_size = "";

        double size = Double.parseDouble(filesize);
        double K = size / 1000;
        double M = K / 1000;

        if (M > 0.1) {
            file_size = String.format("%.2f", M) + "M";
        } else if (M < 0.1 && K > 0.1) {
            file_size = String.format("%.2f", K) + "k";
        } else if (K < 0.1) {
            file_size = size + "b";
        }
        return file_size;
    }

    /**
     * 时间戳转换成日期
     */
    public static String timeStamp2Date(String seconds, String format) {
        if (seconds == null || seconds.isEmpty() || seconds.equals("null")) {
            return "";
        }
        if (format == null || format.isEmpty()) format = "yyyy-MM-dd HH:mm:ss";
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(new Date(Long.valueOf(seconds)));
    }


}
